const sequelize = require("../db/sequelize");
const User = require("./user/user");
const GoodsAttrs = require("./goods/goods_attrs");
const Goods = require("./goods/goods");
const GoodsBanner = require("./goods/goods_banner");
const Category = require("./category/category");
const OrderItem = require("./order/order_item");
const GoodsSkus = require("./goods/goods_skus");
const GoodsSkusCard = require("./goods/goods_skus_card");
const { Op } = require("sequelize");
const GoodsType = require("./goods/goods_type");
const GoodsTypeValue = require("./goods/goods_type_value");
const GoodsSkusCardValue = require("./goods/goods_skus_card_value");
const UserLevel = require("./user/user_level");
const UserInfo = require("./user/user_info");
const UserAddresses = require("./user/user_addresses");
const Order = require("./order/order");
const Cart = require("./cart/cart");
//创建商品关系
async function initGoods() {
    /**
     * 关系笔记
     * hasMany是一 belongsTo是多
     * hasMany里可以设置belongsTo表关联的字段
     */
    //创建商品与分类多对一 
    Category.hasMany(Goods, { foreignKey: "category_id" })
    Goods.belongsTo(Category, { foreignKey: "category_id" })
    //关联商品与评论一对多
    OrderItem.belongsTo(Goods, { foreignKey: "goods_id", as: "goods_item" })
    Goods.hasMany(OrderItem, { foreignKey: "goods_id" })
    //关联商品与规格一对多
    GoodsSkus.belongsTo(Goods, { foreignKey: "goods_id" })
    Goods.hasMany(GoodsSkus, { foreignKey: "goods_id" })
    //商品与规格卡片一对多
    GoodsSkusCard.belongsTo(Goods, { foreignKey: "goods_id" })
    Goods.hasMany(GoodsSkusCard, { foreignKey: "goods_id" })
    //规格卡片与卡片值一对多
    GoodsSkusCardValue.belongsTo(GoodsSkusCard, { foreignKey: "goods_skus_card_id" })
    GoodsSkusCard.hasMany(GoodsSkusCardValue, { foreignKey: "goods_skus_card_id" })

    //创建商品与商品属性一对多
    GoodsAttrs.belongsTo(Goods, { foreignKey: "goods_id" })
    Goods.hasMany(GoodsAttrs, { foreignKey: "goods_id" })
    //创建商品与商品轮播图一对多
    GoodsBanner.belongsTo(Goods, { foreignKey: "goods_id" })
    Goods.hasMany(GoodsBanner, { foreignKey: "goods_id" })
    //商品与goods_type多对一
    Goods.belongsTo(GoodsType, { foreignKey: "type_id" })
    GoodsType.hasMany(Goods, { foreignKey: "type_id" })
    //goods_type与goods_type_value
    GoodsTypeValue.belongsTo(GoodsType, { foreignKey: "goods_type_id" })
    GoodsType.hasMany(GoodsTypeValue, { foreignKey: "goods_type_id" })

}
//初始化用户表关联
async function initUser() {
    //与用户等级关联
    User.belongsTo(UserLevel, { foreignKey: "user_level_id" })
    UserLevel.hasMany(User, { foreignKey: "user_level_id" })
    //与资料关联
    User.hasOne(UserInfo)
    UserInfo.belongsTo(User)
    //与收货地址列表关联
    User.hasMany(UserAddresses, { foreignKey: "user_id" })
    UserAddresses.belongsTo(User, { foreignKey: "user_id" })
    //关联购物车
    User.hasMany(Cart, { foreignKey: "user_id" })
    Cart.belongsTo(User, { foreignKey: "user_id" })
    {
        //关联购物车
        GoodsSkus.hasMany(Cart, { foreignKey: 'goods_skus_id' })
    }
    //关联订单
    User.hasMany(Order, { foreignKey: "user_id" })
    Order.belongsTo(User, { foreignKey: "user_id" })
}
//初始化订单关系
async function initOrder() {
    //关联订单商品
    Order.hasMany(OrderItem, { foreignKey: "order_id" })
    OrderItem.belongsTo(Order, { foreignKey: "order_id" })
}
//初始化评论模型
async function initOrderItem() {
    OrderItem.belongsTo(User, { foreignKey: "user_id" })
    User.hasMany(OrderItem, { foreignKey: "user_id" })

    OrderItem.belongsTo(GoodsSkus, { foreignKey: "goods_skus_id" })
    GoodsSkus.hasMany(OrderItem, { foreignKey: "goods_skus_id" })


}
async function initCart() {
    Cart.belongsTo(GoodsSkus, { foreignKey: "goods_skus_id", targetKey: "id" })
    Cart.belongsTo(Goods, { foreignKey: "goods_id",targetKey:"id"})
}
//初始化关系
module.exports = async function init() {
    await initGoods()
    await initUser()
    await initOrder()
    await initOrderItem()
    await initCart()
    //初始化数据库
    await sequelize.sync({ force: false })
    // Order.getList(76).then(value=>{
    //     console.log(value.totalCount);
    //     console.log(value.list[0]);
    // })
}
